<?php

class IndexController extends Zend_Rest_Controller
{
	protected $db;

        public function init(){
            $this->db = new Application_Model_SimpleDb();
            // Filter and Validate Expected GET/POST Input
            $filters = array(
                '*' => array(
                    'StringTrim'
                )
            );

            $validators = array(   // Send Post Parameters
                    'id'		=> array(),
                    'userId'	=> array(),
                    'deviceId'	=> array(),
                    'devregid'	=> array(),
                    'deviceName'	=> array(),
                    'delete'	=> array()
            );
            $this->params = new Zend_Filter_Input($filters, $validators, $this->_request->getParams());
            //$this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender();
	}

	public function indexAction(){

            $items = $this->db->getAll();
            $formatted_data = array();

            foreach($items->getData() as $id => $attributes){
                $formatted_item = array(
                    "device_id" => $id
                );
                foreach($attributes as $attribute){
                    $values = $attribute->getValues();
                    $formatted_item[$attribute->getName()] = $values[0];
                }
                $formatted_data[] = $formatted_item;
            }
				
            $this->getResponse()
		->setHeader(Zend_Http_Client::CONTENT_TYPE, "application/json")
		->appendBody(Zend_Json_Encoder::encode($formatted_data));
	}

	public function getAction(){
            $id = $this->params->id;
            $attributes = $this->db->get($id);
            $formatted_item = array(
                "device_id" => $id
            );
            foreach($attributes as $attribute){
                $values = $attribute->getValues();
                $formatted_item[$attribute->getName()] = $values[0];
            }
            $this->getResponse()
                ->setHeader(Zend_Http_Client::CONTENT_TYPE, "application/json")
                ->appendBody(Zend_Json_Encoder::encode($formatted_item));
	}

	public function postAction(){
            $user_id	= $this->params->userId;
            $device_id	= $this->params->deviceId;
            $registration_id= $this->params->devregid;
            $device_type= $this->params->deviceName;
            $result     = $this->db->insert($user_id, $device_id, $registration_id, $device_type);

            $this->getResponse()
                ->setHeader(Zend_Http_Client::CONTENT_TYPE, "application/json")
                ->appendBody(Zend_Json_Encoder::encode($result));
        }

        public function putAction(){
            // put not implemented
        }

        public function deleteAction(){
            $device_id = $this->params->id;
            $result = $this->db->deleteItem($device_id);

            $this->getResponse()
                ->setHeader(Zend_Http_Client::CONTENT_TYPE, "application/json")
                ->appendBody(Zend_Json_Encoder::encode($result));
        }

}

